package cn.edu.pzhu.javaweb.pojo;

import java.io.Serializable;

/**
 * 统一响应结果封装类
 * 用于规范前后端数据交互格式
 * 
 * @param <T> 数据泛型
 */
public class Result<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 状态码
     */
    private Integer code;
    
    /**
     * 响应消息
     */
    private String msg;
    
    /**
     * 响应数据
     */
    private T data;
    
    /**
     * 时间戳
     */
    private Long timestamp;
    
    /**
     * 请求路径（可选）
     */
    private String path;
    
    /**
     * 成功状态常量
     */
    public static final int SUCCESS_CODE = 200;
    public static final String SUCCESS_MSG = "操作成功";
    
    /**
     * 错误状态常量
     */
    public static final int ERROR_CODE = 500;
    public static final String ERROR_MSG = "系统异常";
    
    /**
     * 客户端错误常量
     */
    public static final int BAD_REQUEST_CODE = 400;
    public static final String BAD_REQUEST_MSG = "请求参数错误";
    
    /**
     * 未授权常量
     */
    public static final int UNAUTHORIZED_CODE = 401;
    public static final String UNAUTHORIZED_MSG = "未授权";
    
    /**
     * 禁止访问常量
     */
    public static final int FORBIDDEN_CODE = 403;
    public static final String FORBIDDEN_MSG = "禁止访问";
    
    /**
     * 资源不存在常量
     */
    public static final int NOT_FOUND_CODE = 404;
    public static final String NOT_FOUND_MSG = "资源不存在";
    
    // 构造函数
    public Result() {
        this.timestamp = System.currentTimeMillis();
    }
    
    public Result(Integer code, String msg) {
        this();
        this.code = code;
        this.msg = msg;
    }
    
    public Result(Integer code, String msg, T data) {
        this();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    
    // 成功响应的静态方法
    
    /**
     * 成功响应（无数据）
     */
    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, SUCCESS_MSG);
    }
    
    /**
     * 成功响应（带数据）
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, SUCCESS_MSG, data);
    }
    
    /**
     * 成功响应（自定义消息）
     */
    public static <T> Result<T> success(String msg) {
        return new Result<>(SUCCESS_CODE, msg);
    }
    
    /**
     * 成功响应（自定义消息和数据）
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(SUCCESS_CODE, msg, data);
    }
    
    // 错误响应的静态方法
    
    /**
     * 错误响应（默认错误）
     */
    public static <T> Result<T> error() {
        return new Result<>(ERROR_CODE, ERROR_MSG);
    }
    
    /**
     * 错误响应（自定义消息）
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(ERROR_CODE, msg);
    }
    
    /**
     * 错误响应（自定义状态码和消息）
     */
    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(code, msg);
    }
    
    /**
     * 错误响应（自定义状态码、消息和数据）
     */
    public static <T> Result<T> error(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
    
    // 其他常见状态响应
    
    /**
     * 参数错误响应
     */
    public static <T> Result<T> badRequest() {
        return new Result<>(BAD_REQUEST_CODE, BAD_REQUEST_MSG);
    }
    
    public static <T> Result<T> badRequest(String msg) {
        return new Result<>(BAD_REQUEST_CODE, msg);
    }
    
    /**
     * 未授权响应
     */
    public static <T> Result<T> unauthorized() {
        return new Result<>(UNAUTHORIZED_CODE, UNAUTHORIZED_MSG);
    }
    
    public static <T> Result<T> unauthorized(String msg) {
        return new Result<>(UNAUTHORIZED_CODE, msg);
    }
    
    /**
     * 禁止访问响应
     */
    public static <T> Result<T> forbidden() {
        return new Result<>(FORBIDDEN_CODE, FORBIDDEN_MSG);
    }
    
    public static <T> Result<T> forbidden(String msg) {
        return new Result<>(FORBIDDEN_CODE, msg);
    }
    
    /**
     * 资源不存在响应
     */
    public static <T> Result<T> notFound() {
        return new Result<>(NOT_FOUND_CODE, NOT_FOUND_MSG);
    }
    
    public static <T> Result<T> notFound(String msg) {
        return new Result<>(NOT_FOUND_CODE, msg);
    }
    
    // 链式调用方法（可选）
    
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }
    
    public Result<T> msg(String msg) {
        this.msg = msg;
        return this;
    }
    
    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
    
    public Result<T> path(String path) {
        this.path = path;
        return this;
    }
    
    // 判断是否成功的便捷方法
    public boolean isSuccess() {
        return this.code != null && this.code.intValue() == SUCCESS_CODE;
    }
    
    // Getter 和 Setter 方法
    public Integer getCode() {
        return code;
    }
    
    public void setCode(Integer code) {
        this.code = code;
    }
    
    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    public Long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
    
    public String getPath() {
        return path;
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                ", path='" + path + '\'' +
                '}';
    }

	public String getMessage() {
		// TODO Auto-generated method stub
		return null;
	}
}